<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Url;
use think\Session;
use think\Controller;

/**
 * 
 */
class Shop extends Controller
{
	/*店铺列表*/
	public function index()
	{
		if(Request::instance()->isPost()){
			$data = Request::instance()->post('name');
			$list = DB::name('shop')->where("is_del =0 and title like '%".$data."%'")->order('orderid asc')->paginate(20);
		}else{
		$list = DB::name('shop')->where('is_del = 0')->order('orderid asc')->paginate(20);
		}
		$page = $list->render();
		$this->assign('page', $page);
		$list = $list->all();
		foreach ($list as $k => $v) {
			$dizhi = $this->getAddress($v['prov'],$v['city'],$v['area']);
			$list[$k]['dizhi'] = $dizhi.$v['address'];
		}
		return $this->fetch('index',[
			'list'		=>	$list,
		]);
	}
	/*添加店铺*/
	public function create()
	{
		if(Request::instance()->post()){
			$data = Request::instance()->post();
			$data['add_time'] = time();
			$data['is_del'] = 0;
			unset($data['id']);
			$res = DB::name('shop')->insert($data);
			if($res){
				$this->success('新建店铺成功',url('index/index/index',array('con'=>'shop','act'=>'index')));
			} 
		}
		$ord = DB::name('shop')->max('id');
		$ord +=1;
		$this->assign('ord',$ord);
		$param = $this->appendarg();
		return $this->fetch('create', $param);
	}

	/*修改店铺*/
	public function update($id)
	{
		if(Request::instance()->isPost()){
			$data = Request::instance()->post();
			$id = $data['id'];
			unset($data['id']);
			$res = DB::name('shop')->where('id = '.$id)->setField($data);
			if($res){
				$this->success('修改信息成功',url('index/index/index',array('con'=>'shop','act'=>'index')));
			}else{
				$this->error('修改信息失败');
			}
		}
		$this->assign('data',DB::name('shop')->where('id = '.$id)->find());
		$prov = $this->appendarg();
		$this->assign('prov_data',$prov['prov_data']);
		return $this->fetch('create');
	}


	/**
     * 添加修改时候需要传递参数的话，用此方法，只写一遍
     */
	public function appendarg(){
	    $prov_data = db::name('cn_prov')->field('code,name')->select();
		return [
		   'prov_data' =>$prov_data,
		];
	}


	public function getAddress($prov,$city,$area)
	{
		$prov = DB::name('cn_prov')->where('code = '.$prov)->value('name');
		$city = DB::name('cn_city')->where('code = '.$city)->value('name');
		$area = DB::name('cn_area')->where('code = '.$area)->value('name');
		$address = $prov.$city.$area;
		return $address;
	}
	/*删除店铺*/
	public function delshop()
	{
		if(Request::instance()->isPost())
		{
			$id = Request::instance()->post('id');
			/*判断权限*/
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'shop_del');
			if($auth !=true){//没有删除菜单的权限
				$res['sta'] = '-1';
				$res['msg'] = '权限不足';
				
			}else{
				$del = DB::name('shop')->where('id = '.$id)->setField('is_del',1);
				if($del){
					$res['sta'] = '1';
					$res['msg'] = '删除成功';
				}else{
					$res['sta'] = '0';
					$res['msg'] = '删除失败';
				}
			}
			return json($res);
		}
	}


	/*店铺成衣库存列表*/
	public  function clothesList()
	{
		$shopid = DB::name('user')->where('id = '.Session::get('manage_id'))->value('shop_id');
		/*判断权限*/
		$authmenu = new \think\Authmenu();
		$uid = Session::get('manage_id');
		$auth=$authmenu->check($uid,'shop_allkucun');
		if($auth !=true){/*判断有没有查看全部店铺成衣库存的权限*/
			$where['shopid'] = $shopid;
			}
		/*if($shopid > 0){做自己只能看到自己店铺的库存
		}*/
		/*搜索*/
		if(Request::instance()->isPost())
		{
			$name = Request::instance()->post('name');/*搜索 根据名称或者是订单号查询*/
			$aa = preg_match('/[\x{4e00}-\x{9fa5}]/u', $name);/*判断里面有没有汉字*/
                if($aa == 1){/*说明有汉字，那就是暗名称搜*/
                    //$where .=" and title like '%".$name."%'";
                    $code = DB::name('clothes_code')->field('code')->where("name like '%".$name."%'")->select();
                   $arr = "";
                    foreach ($code as $key => $value) {
                    	$arr .= $value['code'].',';
                    }
                    $arr = substr($arr, 0 ,-1);
                    $where['code'] = array('in',$arr);	
                }else{/*没有就是面料编号*/
                    $where['ml_num'] = array('like',"%$name%");
                }
		}
		$where['is_del'] = 0;
		$where['num'] = array('gt',0);
		$list = DB::name('shop_clothes')->where($where)->order('id desc')->paginate(15);
		$count = DB::name('shop_clothes')->where($where)->sum('num');
		$page = $list->render();
		$list = $list->all();
		foreach ($list as $k => $v) {
			$code = DB::name('clothes_code')->where("code = '".$v['code']."'")->find();
			$list[$k]['shop'] = DB::name('shop')->where('id = '.$v['shopid'])->value('title');
			$list[$k]['name'] = $code['name'];
			$list[$k]['price'] = $code['price'];
			$list[$k]['ml_num'] = $code['ml_num'];
			$list[$k]['ml_style'] = $code['ml_style'];
			switch ($code['season']) {
				case '1':
				$list[$k]['season'] = '春季';
				break;
				case '2':
				$list[$k]['season'] = '夏季';
				break;
				case '3':
				$list[$k]['season'] = '秋季';
				break;
				case '4':
				$list[$k]['season'] = '冬季';
				break;	
				
			}
			$list[$k]['category'] = DB::name('clothes_category')->where('id ='.$code['category'])->value('value');
			$list[$k]['color']	  = DB::name('clothes_color')->where('id = ' .$code['color'])->value('value');
			$list[$k]['size']     = DB::name('clothes_size')->where('id = '.$code['size'])->value('value');	
		}
		$shop = DB::name('shop')->where('is_del = 0')->order('id asc')->select();
		return $this->fetch('clothes_list',[
			'list'		=>	$list,
			'page'		=>	$page,
			'count'		=>	$count,
			'shop'		=>	$shop,
		]);
	}

	/*录入店铺成衣库存---->扫码枪扫描*/
	public function addClothes()
	{
		/*判断权限*/
		$authmenu = new \think\Authmenu();
		$uid = Session::get('manage_id');
		$auth=$authmenu->check($uid,'shop_addclothes');
		if($auth !=true)
		{
			$this->error('权限不足');
		}
		return $this->fetch('add_clothes');
	}
	/*ajax的录入店铺成衣库存*/
	public function writeKucun()
	{
		if(request::instance()->isPost())
		{
            $code   =   request::instance()->post('code/a');
            $num    =   request::instance()->post('num/a');
            $entry  =   Session::get('manage_id');
            $shopid =   DB::name('user')->where('id = '.$entry)->value('shop_id');
            //开启数据库的事务  要是有错误要回滚  防止数据库错误
            DB::startTrans();
            try{
                for($i = 0; $i < count($code); $i ++)
                {
                    //先查询下看看有没有 分配记录  没有就不能录入
                    $fenpei = DB::name('xingzheng_fenpei')->where("code = '".$code[$i]."' and shopid = ".$shopid)->value('id');
                    if($fenpei > 0 ){
                     //然后再看下之前有没有这个衣服
                        $cunzai = DB::name('shop_clothes')->field('id,num')->where("code = '".$code[$i]."' and shopid = ".$shopid)->find();
                        if(count($cunzai) >0){//说明存在 直接加数值就行
                            $update['num'] = $cunzai['num']+$num[$i];
                            $res = DB::name('shop_clothes')->where("code = '".$code[$i]."'")->setField($update);
                        }else{
                            $data['num']  = $num[$i];
                            $data['code']   =   $code[$i];
                            $data['ml_num'] = DB::name('clothes_code')->where("code = '".$code[$i]."'")->value('ml_num');
                            $data['addtime'] = time();
                            $data['shopid'] =   $shopid;
                            $data['entry_id'] = $entry;
                            $res = DB::name('shop_clothes')->insert($data);
                        }
                    }else{
                        $this->error('没有分配记录');
                    }
                }
                DB::commit();
            }catch (\Exception $e)
            {
                DB::rollback();
                $this->error('入库失败,没有分配记录');
            }
            $this->success('入库成功',url('index/index/index',array('con'=>'shop','act'=>'clothesList')));
			/*$data['code'] = request::instance()->post('order');
			$data['addtime'] = $update['addtime'] = time();
			$data['entry_id'] = Session::get('manage_id');
			$ml_num = DB::name('clothes_code')->where("code = '".$data['code']."'")->value('ml_num');
			$data['shopid'] = DB::name('user')->where('id = '.$data['entry_id'])->value('shop_id');

			$fenpei = DB::name('xingzheng_fenpei')->where("code = '".$data['code']."' and shopid = ".$data['shopid'])->value('id');

			if($fenpei >0){
				$cunzai = DB::name('shop_clothes')->field('id,num')->where("code = '".$data['code']."' and shopid = ".$data['shopid'])->find();
				if(count($cunzai) >0 ){//说明已经存在
					$update['num'] = $cunzai['num']+1;

					$res = DB::name('shop_clothes')->where("code = '".$data['code']."'")->setField($update);
				}else{
					$data['num']  = 1;
					$data['ml_num'] = $ml_num;
					$data['entry_id'] = Session::get('manage_id');
					$res = DB::name('shop_clothes')->insert($data);
				}
				if($res)
				{
					$rs['msg'] = '录入成功';
					$rs['sus'] = '1';
				}
			}else{
				$rs['msg'] = '没有分配记录';
				$rs['sus'] = '-2';
			}
			return json($rs);*/

		}
	}

	/*店铺成衣出库页面*/
	public function outbound()
	{
		/*判断权限*/
		$authmenu = new \think\Authmenu();
		$uid = Session::get('manage_id');
		$auth=$authmenu->check($uid,'shop_outbound');
		if($auth !=true)
		{
			$this->error('权限不足');
		}else{

			if(Request::instance()->isPost())
			{
				$where['code'] = request::instance()->post('order');
				$entry_id = Session::get('manage_id');
				$where['shopid'] = DB::name('user')->where('id = '.$entry_id)->value('shop_id');
				$where['is_del'] = 0 ;
				$where['num'] = array('gt',0);
				$data = DB::name('shop_clothes')->where($where)->find();
				if($data){
					$code = DB::name('clothes_code')->where("code = '".$data['code']."'")->find();
					$data['name'] = $code['name'];
					$data['price'] = $code['price'];
					$data['sex']	= $code['sex'] == 8 ? '男' : '女' ;
					$data['ml_num'] = $code['ml_num'];
					$data['ml_style'] = $code['ml_style'];
					switch ($code['season']) {
						case '1':
						$data['season'] = '春季';
						break;
						case '2':
						$data['season'] = '夏季';
						break;
						case '3':
						$data['season'] = '秋季';
						break;
						case '4':
						$data['season'] = '冬季';
						break;	
					}
					$data['category'] = DB::name('clothes_category')->where('id ='.$code['category'])->value('value');
					$data['color']	  = DB::name('clothes_color')->where('id = ' .$code['color'])->value('value');
					$data['size']     = DB::name('clothes_size')->where('id = '.$code['size'])->value('value');	
					$data['shop']	  = DB::name('shop')->where('id = '.$data['shopid'])->value('title');
					$data['sus'] = 1;
				}else{
					$data['sus'] = 2;
				}
				return json($data);
			}
		}
		return $this->fetch(); 
	}

	/*店铺出库的方法    要减去店铺库存  要减去行政库存*/
	public function chuku()
	{

		if(Request::instance()->isPost())
		{
			$arr = array();
			$num = request::instance()->post('num/a');
			$code = request::instance()->post('code/a');
			$sale_price = request::instance()->post('sale_price/a');
			$payment	=	request::instance()->post('payment');
			$shop = DB::name('user')->where('id = '.Session::get('manage_id'))->value('shop_id');
			$total = request::instance()->post('total');
			$card_num	=	request::instance()->post('card_num');
			DB::startTrans();
			/*会员卡支付要添加会员卡消费记录 在添加消费记录之前先判断下余额情况*/
			if($payment == 0)
			{
				$money	=	DB::name('member')->where('status = 1 and is_del = 0 and card_num = '.$card_num)->value('money');
				if($money < $total){
					$this->error('会员卡余额不足，请先去充值');
					exit();
				}
			}
				try{
					for($i = 0; $i< count($num); $i++)
					{
						$sheng = DB::name('shop_clothes')->where("is_del = 0  and shopid = ".$shop." and code = '".$code[$i]."'")->value('num');
						if($sheng < $num[$i])
						{
							$this->error('有货物库存不足');
							exit();
						}else{
							$dian = DB::name('shop_clothes')->where("is_del = 0  and shopid = ".$shop." and code = '".$code[$i]."'")->setDec('num',$num[$i]);//先减少本店店铺的库存*
							//核减行政库存
							$xingzheng = DB::name('xingzheng_kucun')->where("is_del = 0 and code = '".$code[$i]."'")->setDec('shengyu_num',$num[$i]);
							//添加出库记录
							$data['ml_num'] = DB::name('clothes_code')->where("code ='".$code[$i]."'")->value('ml_num');
							$data['price']  = DB::name('clothes_code')->where("code ='".$code[$i]."'")->value('price');
							$data['sale_price'] = $sale_price[$i];
							$data['addtime'] = time();
							$data['entry_id'] = Session::get('manage_id');
							$data['shopid'] = DB::name('user')->where('id ='.Session::get('manage_id'))->value('shop_id');
							$data['is_del'] = 0;
							$data['num']	=	$num[$i];
							$data['code'] = $code[$i];
							$data['payment'] = $payment;
							$res = DB::name('shop_clothes_row')->insert($data);
							/*会员卡支付要添加会员卡消费记录 在添加消费记录之前先判断下余额情况*/
							if($payment == 0)
							{
								/*添加消费记录*/
								$buy['card_num']	=	$card_num;
								$buy['code']		=	$code[$i];
								$buy['money']		=	$sale_price[$i];
								$buy['entry_id']	=	$data['entry_id'];
								$buy['shop_id']		=	$shop;
								$buy['add_time']	=	time();
								$row = DB::name('member_buyrow')->insert($buy);
							}
						}
					}
					if($payment == 0)
					{
						$change	=	DB::name('member')->where('card_num ='.$card_num)->setDec('money',$total);
					}
					Db::commit();
				}catch (\Exception $e)
				{
					dump($e->getMessage());
					Db::rollback();
					$this->error('出库失败,原因是有货物库存不足');
				}
			$this->success('出库成功',url('index/index/index',array('con'=>'shop','act'=>'clothesRow')));
			}

	}


	/*成衣出库记录*/
	public function clothesRow()
	{
		if(Request::instance()->isPost())
		{
			$name = Request::instance()->post('name');
			 $aa = preg_match('/[\x{4e00}-\x{9fa5}]/u', $name);/*判断里面有没有汉字*/
                if($aa == 1){/*说明有汉字，那就是暗名称搜*/
                    //$where .=" and title like '%".$name."%'";
                    $code = DB::name('clothes_code')->field('code')->where("name like '%".$name."%'")->select();
                   $arr = "";
                    foreach ($code as $key => $value) {
                    	$arr .= $value['code'].',';
                    }
                    $arr = substr($arr, 0 ,-1);
                    $where['code'] = array('in',$arr);	
                }else{/*没有就是面料编号*/
                    $where['ml_num'] = array('like',"%$name%");
                }
		}
		$where['is_del'] = 0;
		$list = DB::name('shop_clothes_row')->where($where)->order('id desc')->paginate(15);
		$page = $list->render();
		$list = $list->all();
		foreach ($list as $k => $v) {
			$code = DB::name('clothes_code')->where("code = '".$v['code']."'")->find();
			$list[$k]['shop'] = DB::name('shop')->where('id = '.$v['shopid'])->value('title');
			$list[$k]['name'] = $code['name'];
			$list[$k]['price'] = $code['price'];
			$list[$k]['ml_num'] = $code['ml_num'];
			$list[$k]['ml_style'] = $code['ml_style'];
			$list[$k]['category'] = DB::name('clothes_category')->where('id ='.$code['category'])->value('value');
			$list[$k]['color']	  = DB::name('clothes_color')->where('id = ' .$code['color'])->value('value');
			$list[$k]['size']     = DB::name('clothes_size')->where('id = '.$code['size'])->value('value');
			$list[$k]['entry']    = DB::name('user')->where('id ='.$v['entry_id'])->value('truename');	
		}
		return $this->fetch('clothes_row',[
			'list'		=>	$list,
			'page'		=>	$page,
		]);
	}


	/*ajax 删除店铺库存 --- 假删除   只修改状态 不删除数据*/
	public function delclothes()
	{
		if(Request::instance()->isPost())
		{
			$id = Request::instance()->post('id');
			/*判断权限*/
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'shop_delclothes');
			if($auth !=true){//没有删除菜单的权限
				$res['sta'] = '-1';
				$res['msg'] = '权限不足';
				
			}else{
				$del = DB::name('shop_clothes')->where('id = '.$id)->setField('is_del',1);
				if($del){
					$res['sta'] = '1';
					$res['msg'] = '删除成功';
				}else{
					$res['sta'] = '0';
					$res['msg'] = '删除失败';
				}
			}
			return json($res);
		}	
	}

	/*ajax 删除店铺销售记录--- 假删除   只修改状态 不删除数据*/
	public function delClothesRow()
	{
		if(Request::instance()->isPost())
		{
			$id = Request::instance()->post('id');
			/*判断权限*/
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'shop_delClothesRow');
			if($auth !=true){//没有删除菜单的权限
				$res['sta'] = '-1';
				$res['msg'] = '权限不足';
				
			}else{
				$del = DB::name('shop_clothes_row')->where('id = '.$id)->setField('is_del',1);
				if($del){
					$res['sta'] = '1';
					$res['msg'] = '删除成功';
				}else{
					$res['sta'] = '0';
					$res['msg'] = '删除失败';
				}
			}
			return json($res);
		}	
	}

	/*导出店铺成衣库存的方法*/
	public function excel()
	{
		$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'shop_clothes_daochu');
			if($auth != true){
				$this->error('权限不足');
		}
		
		$name='成衣库存列表';
		$header=['名称','条码编号','所属季节','所属品类','颜色','号型','价格','面料编号','面料属性','所属店铺','库存','入库时间'];
		$shopid = DB::name('user')->where('id = '.Session::get('manage_id'))->value('shop_id');
		

		/*//判断权限
		$authmenu = new \think\Authmenu();
		$uid = Session::get('manage_id');
		$auth=$authmenu->check($uid,'shop_allkucun');
		if($auth !=true){//判断有没有查看全部店铺成衣库存的权限
			$where['shopid'] = $shopid;
			}*/
		if(Request::instance()->isPost())
		{
			$shopid = Request::instance()->post('shopid');
			if($shopid > 0){
				$where['shopid'] = $shopid;
			}
		}
			$where['is_del'] = 0;
			/*获取到id 然后查询这些id所对应的信息 最后放在表格里面*/
			$list = DB::name('shop_clothes')->where($where)->order('id desc')->select();
			$str = array();
			foreach ($list as $k => $v) {
				$code = DB::name('clothes_code')->where("code = '".$v['code']."' ")->find();
				$v['name'] = $code['name'];
				$v['price'] = $code['price'];
				$v['ml_style'] = $code['ml_style'];
				$v['shop'] = DB::name('shop')->where('id = '.$v['shopid'])->value('title');
				$v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
				switch ($code['season']) {
					case '1':
					$v['season'] = '春季';
					break;
					case '2':
					$v['season'] = '夏季';
					break;
					case '3':
					$v['season'] = '秋季';
					break;
					case '4':
					$v['season'] = '冬季';
					break;	
				}
				$v['category'] = DB::name('clothes_category')->where('id ='.$code['category'])->value('value');
				$v['color']	   = DB::name('clothes_color')->where('id = ' .$code['color'])->value('value');
				$v['size']     = DB::name('clothes_size')->where('id = '.$code['size'])->value('value');	
				$str[] = [$v['name'],$v['code'],$v['season'],$v['category'],$v['color'],$v['size'],$v['price'],$v['ml_num'],$v['ml_style'],$v['shop'],$v['num'],$v['addtime']];
			}
			$data=$str;
			excelExport($name,$header,$data);
		
	}

	public function xiugai()
	{
		$list = DB::name('shop_clothes')->select();
		
		$a = substr($list[0]['code'], 9);
		foreach ($list as $k => $v) {
		 $a = substr($v['code'], 9);
		 $res = DB::name('shop_clothes')->where('id = '.$v['id'])->setField('ml_num',$a);	
		}
	}

	/*=====================================成衣库存盘点开始=======================================================*/

	/*店铺成衣库存盘点方法*/
	public function pandian()
	{
		if(Request::instance()->isPost())
		{
			/*先获取编号 在根据编号查询详情 再根据编号查询线上的剩余数量*/
			$code = Request::instance()->post('order');
			/*名称  颜色 	号型 	价格 	面料编号  店铺 数量*/
			/*获取到本月第一天的时间戳*/
			$data['month'] = $month = strtotime(date('Y-m-01 0:00:00', strtotime(date("Y-m-d"))));
			$data['entry_id'] = $entry_id  =  Session::get('manage_id');
			$data['shopid'] = $shopid   =  DB::name('user')->where('id = '.$entry_id)->value('shop_id');
			$data['addtime'] = $data['updatetime'] = time();
			$num = DB::name('pandian_info')->where("shopid =".$shopid." and month = ".$month." and code = '".$code."'")->value('id');
			if($num > 0){/*大于说明同月份同店同件产品已经录过了 只要添加数量就行了*/
				$res = DB::name('pandian_info')->where('id = '.$num)->setInc('sp_num',1);
				if($res)
				{
					$listId = DB::name('pandian_list')->where("month = ".$month." and shopid = ".$shopid)->value('id');
					if($listId > 0){
						$list = DB::name('pandian_list')->where('id = '.$listId)->setField('updatetime',$data['updatetime']);
					}else{
						$list = DB::name('pandian_list')->insert($data);
					}
					if($list)
					{
						$result['sus'] = 1;
						$result['msg'] = '成功';
					}
				}else{
					$result['sus'] = 0;
					$result['msg'] = '失败';
				}
				return json($result);
			}else{/*说明数据库还没有本月的该店的盘点记录 需要新增一条*/
				$pandian = 	 DB::name('shop_clothes')
								->alias('a')
								->join('clothes_code b','a.code = b.code')
								->field(['b.name','b.ml_num','b.category','b.color','b.price','b.size','a.num as ol_num'])
								->where("a.is_del = 0 and a.shopid = ".$shopid." and a.code = '".$code."' ")
								->find();	

				$pandian['code'] = $code;				
				$pandian['entry_id'] = $entry_id;
				$pandian['shopid'] = $shopid;
				$pandian['month'] = $data['month'] = $month;				
				$pandian['sp_num'] = 1;
				$pandian['addtime'] =  time();
				$res = DB::name('pandian_info')->insert($pandian);
				if($res)
				{
					$listId = DB::name('pandian_list')->where("month = ".$month." and shopid = ".$shopid)->value('id');
					if($listId > 0){
						$list = DB::name('pandian_list')->where('id = '.$listId)->setField('updatetime',$data['updatetime']);
					}else{
						$list = DB::name('pandian_list')->insert($data);
					}
					if($list)
					{
						$result['sus'] = 1;
						$result['msg'] = '成功';
					}
				}else{
					$result['sus'] = 0;
					$result['msg'] = '失败';
				}
				return json($result);
			}
		}else{
			return $this->fetch();
		}
	}

	/*店铺盘点记录列表方法*/
	public function pandianList()
	{
		if(Request::instance()->isPost())/*做筛选  按照店铺来筛选盘点记录*/
		{
			$name  = Request::instance()->post('name');
			$shop  = DB::name('shop')->field('id')->where("title like '%".$name."%'")->select();
			$shopid = '';
			foreach ($shop as $k => $v) {
				$shopid .= $v['id'].',';
			}
			$shopid = substr($shopid, 0 , -1);
			$where['shopid'] = array('in',$shopid);
		}
		$where['is_del'] = 0;
		$list = DB::name('pandian_list')->where($where)->order('id desc')->paginate(15);
		$page = $list->render();
		$list = $list->all();
		foreach ($list as $k => $v) {
			$list[$k]['shop'] = DB::name('shop')->where('id = '.$v['shopid'])->value('title');
			$list[$k]['entry']= DB::name('user')->where('id ='.$v['entry_id'])->value('truename');
		}
		return $this->fetch('pandian_list',[
			'list'		=>	$list,
			'page'		=>	$page,
		]);
	}

	/*店铺盘点记录详情列表*/
	public function showList($shopid,$month)
	{
		if(Request::instance()->isPost())
		{
			$where['ml_num'] = Request::instance()->post('name');
		}
		$where['shopid'] = $shopid;
		$where['month'] = $month;
		$where['is_del'] = 0;
		$list = DB::name('pandian_info')->where($where)->order('id desc')->paginate(15);
		$page = $list->render();
		$list = $list->all();
		foreach ($list as $k => $v) {
			$list[$k]['category'] = DB::name('clothes_category')->where('id ='.$v['category'])->value('value');
			$list[$k]['color']	  = DB::name('clothes_color')->where('id = ' .$v['color'])->value('value');
			$list[$k]['size']     = DB::name('clothes_size')->where('id = '.$v['size'])->value('value');
			$list[$k]['entry']    = DB::name('user')->where('id ='.$v['entry_id'])->value('truename');	
			$list[$k]['shop']	  = DB::name('shop')->where('id ='.$v['shopid'])->value('title');	
		}
		$ol_num = DB::name('pandian_info')->where($where)->sum('ol_num');
		$sp_num = DB::name('pandian_info')->where($where)->sum('sp_num');
		return $this->fetch('show_list',[
			'list'		=>	$list,
			'page'		=>	$page,
			'shopid'	=>	$shopid,
			'month'		=>	$month,
			'ol_num'	=>	$ol_num,
			'sp_num'	=>	$sp_num,
		]);
	}

	/*列表页面的导出数据*/
	public function listExport($attr)
	{
		/*先判断下有没有权限*/
		$authmenu = new \think\Authmenu();
		$uid = Session::get('manage_id');
		$auth=$authmenu->check($uid,'export_pandian');
		if($auth !=true){//判断有没有查看全部店铺成衣库存的权限
			$this->error('权限不足');
			exit();
		}else{
			$where['id'] = array('in',$attr);
			$where['is_del'] = 0;
			$shopid = DB::name('pandian_list')->field('shopid')->where($where)->select();
			$month = DB::name('pandian_list')->field('month')->where($where)->select();
			foreach ($shopid as $k => $v) {
				$shop = join(',',$v);
				$s[] = $shop;
			}
			foreach ($month as $k => $v) {
				$month = join(',',$v);
				$m[] = $month;
			}
			$shopid = implode(',', $s);
			$month  = implode(',', $m);
			$tiaojian['shopid'] = array('in',$shopid);
			$tiaojian['month']  = array('in',$month); 
			$tiaojian['is_del'] = 0;
			$list = DB::name('pandian_info')->where($tiaojian)->order('addtime desc')->select();
			$name='成衣库存盘点列表';
			$header=['序号','店铺名称','盘点员','名称','条码编号','所属品类','颜色','号型','价格','面料编号','线上库存','店铺库存','所属月份','盘点时间'];
			foreach ($list as $k => $v) {
				$v['shop'] = DB::name('shop')->where('id ='.$v['shopid'])->value('title');
				$v['entry']= DB::name('user')->where('id ='.$v['entry_id'])->value('truename');
				$v['category'] = DB::name('clothes_category')->where('id ='.$v['category'])->value('value');
				$v['color']	  = DB::name('clothes_color')->where('id = ' .$v['color'])->value('value');
				$v['size']     = DB::name('clothes_size')->where('id = '.$v['size'])->value('value');
				$v['month'] = date("Y-m",$v['month']);
				$v['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
			$str[] = [$k+1,$v['shop'],$v['entry'],$v['name'],$v['code'],$v['category'],$v['color'],$v['size'],$v['price'],$v['ml_num'],$v['ol_num'],$v['sp_num'],$v['month'],$v['addtime']];
			}
			$data=$str;
			excelExport($name,$header,$data);
		}
	}
	/*详情页面的导出数据*/
	public function showExport($shopid,$month)
	{
		/*先判断下有没有权限*/
		$authmenu = new \think\Authmenu();
		$uid = Session::get('manage_id');
		$auth=$authmenu->check($uid,'export_pandian');
		if($auth !=true){//判断有没有查看全部店铺成衣库存的权限
			$this->error('权限不足');
			exit();
		}else{
			$name='成衣库存盘点列表';
			$header=['序号','店铺名称','盘点员','名称','条码编号','所属品类','颜色','号型','价格','面料编号','线上库存','店铺库存','所属月份','盘点时间'];
			$where['shopid'] = $shopid;
			$where['month'] = $month;
			$where['is_del'] = 0;
			$list = DB::name('pandian_info')->where($where)->order('addtime desc')->select();
			foreach ($list as $k => $v) {
				$v['shop'] = DB::name('shop')->where('id ='.$v['shopid'])->value('title');
				$v['entry']= DB::name('user')->where('id ='.$v['entry_id'])->value('truename');
				$v['category'] = DB::name('clothes_category')->where('id ='.$v['category'])->value('value');
				$v['color']	  = DB::name('clothes_color')->where('id = ' .$v['color'])->value('value');
				$v['size']     = DB::name('clothes_size')->where('id = '.$v['size'])->value('value');
				$v['month'] = date("Y-m",$v['month']);
				$v['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
			$str[] = [$k+1,$v['shop'],$v['entry'],$v['name'],$v['code'],$v['category'],$v['color'],$v['size'],$v['price'],$v['ml_num'],$v['ol_num'],$v['sp_num'],$v['month'],$v['addtime']];
			}
			$data=$str;
			excelExport($name,$header,$data);
		}
	}
	/*列表页面的删除数据*/
	public function delPandianList()
	{
		if(Request::instance()->isPost())
		{
			/*先判断是否有删除权限*/
			$id = Request::instance()->post('id');
			/*判断权限*/
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'del_pandianList');
			if($auth !=true){//没有删除菜单的权限
				$res['sta'] = '-1';
				$res['msg'] = '权限不足';
				
			}else{
				$del = DB::name('pandian_list')->where('id = '.$id)->setField('is_del',1);
				if($del){
					$res['sta'] = '1';
					$res['msg'] = '删除成功';
				}else{
					$res['sta'] = '0';
					$res['msg'] = '删除失败';
				}
				return json($res);
			}
		}
	}
	/*详情页面的删除数据*/
	public function delPandianInfo()
	{
		if(Request::instance()->isPost())
		{
			/*先判断是否有删除权限*/
			$id = Request::instance()->post('id');
			/*判断权限*/
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'del_pandianInfo');
			if($auth !=true){//没有删除菜单的权限
				$res['sta'] = '-1';
				$res['msg'] = '权限不足';
				
			}else{
				$del = DB::name('pandian_info')->where('id = '.$id)->setField('is_del',1);
				if($del){
					$res['sta'] = '1';
					$res['msg'] = '删除成功';
				}else{
					$res['sta'] = '0';
					$res['msg'] = '删除失败';
				}
				return json($res);
			}
		}
	}
	/*=====================================店铺每月业绩指标模块================================================*/	
	/*每月店铺业绩列表*/
	public function yeji()
	{
		/*搜索 暗店铺搜索*/
		if(request::instance()->isPost())
		{
			$name	=	request::instance()->post('name');
			$shop  = DB::name('shop')->field('id')->where("title like '%".$name."%'")->select();
			var_dump($shop);
			$shopid = '';
			foreach ($shop as $k => $v) {
				$shopid .= $v['id'].',';
			}
			$shopid = substr($shopid, 0 , -1);
			$where['shopid'] = array('in',$shopid);
		}
		$where['is_del']	=	0;
		$list 	=	DB::name('shop_yeji')->where($where)->order('id desc')->paginate(15);
		$page	=	$list->render();
		$list	=	$list->all();
		foreach($list as $k=>$v)
		{
			$list[$k]['shop']	=	DB::name('shop')->where('id ='.$v['shopid'])->value('title');
			$list[$k]['entry']	=	DB::name('user')->where('id ='.$v['entry'])->value('truename');
		}
		return $this->fetch('yeji',[
			'list'	=>	$list,
			'page'	=>	$page,
		]);
	}

	/*发布店铺业绩*/
	public function  createYeji()
	{
		if(Request::instance()->isPost())
		{
			$data = Request::instance()->post();
			$data['entry']	=	Session::get('manage_id');
			$data['addtime'] 	=	time();
			$data['month']	=	mktime(0,0,0,date('m'),1,date('Y'));//月初第一天时间戳
			$ye 	=	DB::name('shop_yeji')->where('shopid = '.$data['shopid'].' and month ='.$data['month'])->value('id');
			if($ye >0)
			{
				$this->error('本月以为该店铺创建过业绩指标');
				exit();
			}else{
				$res	=	DB::name('shop_yeji')->insert($data);
				if($res)
				{
					$this->success('创建成功',url('index/index/index',array('con'=>'shop','act'=>'yeji')));
				}else{
					$this->error('创建失败');
				}
			}
		}
		$shop 	=	DB::name('shop')->field('id,title')->where('status = 1 and is_del = 0')->order('id asc')->select();
		return $this->fetch('create_yeji',[
			'shop'	=>	$shop,
		]);
	}
	/*修改发布的店铺业绩*/
	public  function updateYeji($id)
	{
		if(request::instance()->isPost())
		{
			$data	=	request::instance()->post();
			unset($data['shopid']);
			$data['entry']	=	Session::get('manage_id');
			$data['addtime'] 	=	time();
			$res 	=	DB::name('shop_yeji')->where('id ='.$id)->setField($data);
			if($res)
			{
				$this->success('修改成功',url('index/index/index',array('con'=>'shop','act'=>'yeji')));
			}else{
				$this->error('修改失败');
			}
		}
		$shop 	=	DB::name('shop')->field('id,title')->where('status = 1 and is_del = 0')->order('id asc')->select();
		$data	=	DB::name('shop_yeji')->where('id ='.$id)->find();
		return $this->fetch('create_yeji',[
			'data'	=>	$data,
			'shop'	=>	$shop,
		]);
	}

	/*删除店铺业绩指标 只改编状态， 不删除数据*/
	public  function delShopYeji()
	{
		if(request::instance()->isPost())
		{
			$id = Request::instance()->post('id');
			/*判断权限*/
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'del_yeji');
			if($auth !=true){//没有删除的权限
				$res['sta'] = '-1';
				$res['msg'] = '权限不足';

			}else{
				$del = DB::name('shop_yeji')->where('id = '.$id)->setField('is_del',1);
				if($del){
					$res['sta'] = '1';
					$res['msg'] = '删除成功';
				}else{
					$res['sta'] = '0';
					$res['msg'] = '删除失败';
				}
			}
			return json($res);
		}
	}
	/*查询什么面料卖的最好*/
	public function getML()
    {
        $list   =   DB::name('order')
                        ->alias('a')
                        ->join('category b','a.category_id = b.id')
                        ->field("a.ml_num,COUNT(a.id) as num,b.title,DATE_FORMAT(FROM_UNIXTIME(a.add_time,'%Y-%m-%d %H:%i:%s'), '%m-%d') AS days")
                        ->where('is_del = 0')
                        ->group('a.ml_num')
                        ->select();
        var_dump(DB::name('order')->getLastSql()).die();
    }
    /*查询店铺分配记录  按天分配  然后在查看详细信息*/
    public function getRowByDay()
    {
        $where['a.is_del'] = 0 ;
        $list   =   DB::name('xingzheng_fenpei')
                        ->alias('a')
                        ->join('user c','a.entry_id = c.id')
                        ->join('shop d','a.shopid = d.id')
                        ->field("SUM(a.num) as num,c.truename,d.title as shop,DATE_FORMAT(FROM_UNIXTIME(a.addtime,'%Y-%m-%d %H:%i:%s'), '%m-%d') as days")
                        ->where($where)
                        ->group("days,a.shopid")
                        ->order('days desc,a.shopid desc')
                        ->select();
        var_dump(DB::name('xingzheng_fenpei')->getLastSql()).die();
    }
}
?>